package fctuc.dei.myTwitter.server;

import java.nio.channels.SelectionKey;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import fctuc.dei.myTwitter.com.Message;
import fctuc.dei.myTwitter.com.MessageHandler;
import fctuc.dei.myTwitter.com.RMIClientListener;
import fctuc.dei.myTwitter.com.RMIServerConnection;

public class RMIServerConnectionImpl extends UserConnection implements RMIServerConnection {

	RMIClientListener client;

	public RMIServerConnectionImpl(MessageHandler mh, RMIClientListener client)
	throws java.rmi.RemoteException {

		super(mh);
		UnicastRemoteObject.exportObject(this);
		this.client = client;
	}

	public void handleMessage(Message m) throws RemoteException {
		receive(m);
	}

	@Override
	public boolean realSend(Message m) {
		try{
			if (client != null)
				client.handleMessage(m);
			System.err.println(">>> (RMI) "+user+" \n"+ m);

		} catch (RemoteException ioe) {
			ioe.printStackTrace();
			System.err.println(">>> Error sending (RMI) "+user+" \n"+ m);
			return false;
		}

		return true;
	}
	
	@Override
	public void realTerminate() {
		// ??
	}

	@Override
	public void noConnectionWarning() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean realSend(Message m, SelectionKey key) {
		// TODO Auto-generated method stub
		return false;
	}
}
